﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Add_Kosei : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;   

        public frm_Add_Kosei()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            this.partType.Text = "P";
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;
            string new_comp = null;
            string new_part = null;
            float new_quantity = 0;
            string new_type = null;
            string note = null;
            note = this.noteValue.Text;

            if (string.IsNullOrEmpty(this.compID.Text))
            {
                error = "Please enter CompID";
            }
            else
            {
                new_comp = this.compID.Text;
                if (IsExistKosei(new_comp))
                {
                    error = "Comp ID has existed";
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.partNo.Text))
                {
                    error = "Please enter Part No";
                }
                else
                {
                    new_part = this.partNo.Text;
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (new_comp == new_part)
                {
                    error = "Part No must be different from Comp ID";
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.quantityValue.Text))
                {
                    error = "Please enter Quantity";
                }
                else
                {
                    if (float.TryParse(this.quantityValue.Text, out new_quantity))
                    {
                        if (new_quantity < 0)
                        {
                            error = "The Quantity must be greater than 0";
                        }
                    }
                    else
                    {
                        error = "Invalid Quantity";
                    }
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.partType.Text))
                {
                    error = "Please enter Part Type";
                }
                else
                {
                    new_type = this.partType.Text;
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [222_Kosei2] (CompID, PartNo, Quantity, PartType, Note)"
                        + " VALUES ('" + new_comp + "', '" + new_part + "', " + new_quantity + ","
                        + " '"+ new_type + "', '" + note + "')";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);                    
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Kosei added success.");
                    this.Dispose();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot update Kosei. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        /////////////////////////////////////
        /// Check exist Kosei
        /////////////////////////////////////
        private bool IsExistKosei(string comp)
        {
            bool ret = false;
            
            try
            {
                myConnection.Open();
                string sql = "SELECT COUNT(CompID) FROM VKosei WHERE CompID='" + comp + "'";
                SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                if (dataReader.Read())
                {
                    if (dataReader[0] != DBNull.Value)
                    {
                        if (Int32.Parse(dataReader[0].ToString()) > 0)
                        {
                            ret = true;
                        }
                    }
                }                
                cmd_sql.Dispose();
                myConnection.Close();                
            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot check exist Kosei. Please try again.");
            }

            return ret;
        }
    }
}
